package com.attendcollatesys.service.impl;

import com.attendcollatesys.entity.AtMonthTotalsPO;
import com.attendcollatesys.entity.dto.AtMonthTotalsDTO;
import com.attendcollatesys.entity.vo.AtMonthTotalsVO;
import com.attendcollatesys.ex.handler.ex.MessageNullValueException;
import com.attendcollatesys.mapper.AtMonthTotalsMapper;
import com.attendcollatesys.service.AtMonthTotalsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AtMonthTotalsServiceImpl implements AtMonthTotalsService {

    @Autowired
    private AtMonthTotalsMapper atMonthTotalsMapper;

    @Override
    public int insertBatch(List<AtMonthTotalsPO> list) {
        return atMonthTotalsMapper.insertBatch(list);
    }

    @Override
    public int deleteByserialNo(List<String> list) {
        return atMonthTotalsMapper.deleteByserialNo(list);
    }

    @Override
    public AtMonthTotalsVO queryByDateAndNameAndRtx(int pageNum, int pageSize, Map pamas) throws MessageNullValueException {
        PageHelper.startPage(pageNum, pageSize);

        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat outputFormat2 = new SimpleDateFormat("yyyyMM");

        Date date = null;
        String name = (pamas.get("name") == null ? "":(String)pamas.get("name") );
        String rtxNumber = (pamas.get("rtxNumber") == null ? "":(String)pamas.get("rtxNumber") );

        try {
            date = outputFormat.parse(pamas.get("currentMonth") == null ? "":(String)pamas.get("currentMonth") );
        } catch (ParseException e) {
            throw new MessageNullValueException("日期为："+ date + "，日期格式转化出现异常！");
        }

        List<AtMonthTotalsPO> atMonthTotalsPOList = atMonthTotalsMapper.queryByDateAndNameAndRtx(date,name,rtxNumber);
        PageInfo<AtMonthTotalsPO> pageInfo = new PageInfo<>(atMonthTotalsPOList);
        List<AtMonthTotalsDTO> atMonthTotalsDTOS = new ArrayList<>();
        for (int i = 0; i < pageInfo.getList().size(); i++) {
            AtMonthTotalsDTO atMonthTotalsDTO =new AtMonthTotalsDTO();
            atMonthTotalsDTO.setSerialNo(pageInfo.getList().get(i).getSerialNo());
            atMonthTotalsDTO.setAttendanceDate(outputFormat2.format(pageInfo.getList().get(i).getAttendanceDate()));
            atMonthTotalsDTO.setWorkDay(pageInfo.getList().get(i).getWorkDay());
            atMonthTotalsDTO.setSectionMID(pageInfo.getList().get(i).getSectionMID());
            atMonthTotalsDTO.setName(pageInfo.getList().get(i).getName());
            atMonthTotalsDTO.setRtxNumber(pageInfo.getList().get(i).getRtxNumber());
            atMonthTotalsDTO.setClockDay(pageInfo.getList().get(i).getClockDay());
            atMonthTotalsDTO.setExpectedWorkHours(pageInfo.getList().get(i).getExpectedWorkHours());
            atMonthTotalsDTO.setAbsenceWorkHours(pageInfo.getList().get(i).getAbsenceWorkHours());
            atMonthTotalsDTO.setAbsentWVorkHours(pageInfo.getList().get(i).getAbsentWVorkHours());
            atMonthTotalsDTO.setLateTimes(pageInfo.getList().get(i).getLateTimes());
            atMonthTotalsDTO.setLateWorkHours(pageInfo.getList().get(i).getLateWorkHours());
            atMonthTotalsDTO.setLateWorkHours2(pageInfo.getList().get(i).getLateWorkHours2());
            atMonthTotalsDTO.setOvertimeWorkHours(pageInfo.getList().get(i).getOvertimeWorkHours());
            atMonthTotalsDTO.setAttendanceHours(pageInfo.getList().get(i).getAttendanceHours());
            atMonthTotalsDTO.setAttendanceDays(pageInfo.getList().get(i).getAttendanceDays());
            atMonthTotalsDTO.setAttendanceMonth(pageInfo.getList().get(i).getAttendanceMonth());
            atMonthTotalsDTOS.add(atMonthTotalsDTO);
        }

        AtMonthTotalsVO atMonthTotalsVO =new AtMonthTotalsVO(String.valueOf(pageInfo.getTotal()),atMonthTotalsDTOS);



        return atMonthTotalsVO;
    }

}
